//
//  SWSquareController.swift
//  新浪微博
//
//  Created by Mac on 17/1/3.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit
import pop

private let reuseIdentifier = "squareCell"

@objc protocol SWSquareControllerDelegate {
    @objc func startComposeController()
}

class SWSquareController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    var delegate: SWSquareControllerDelegate?
    var collectionView: UICollectionView?
    
    let sections:Int = 2
    let items:Int = 3
    var cellArray: [SWSquareViewCell]? = []
    // 在懒加载中,可以省略闭包中的参数部分
    lazy var cellDTOArray:[SWComposeDTO] = {
        
        let array = [["imageName":"tabbar_compose_idea", "title":"文字"],
                     ["imageName":"tabbar_compose_photo", "title":"相册/视频"],
                     ["imageName":"tabbar_compose_weibo", "title":"文章"],
                     ["imageName":"tabbar_compose_lbs", "title":"签到"],
                     ["imageName":"tabbar_compose_review", "title":"点评"],
                     ["imageName":"tabbar_compose_more", "title":"更多"]]
        let arrayDTO:[SWComposeDTO] = SWComposeDTO.getDTOArray(array: array)
        return arrayDTO
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: SWSquareLayout())
        
        view.addSubview(collectionView!)
        
        let nib:UINib = UINib.init(nibName: "SWSquareViewCell", bundle: Bundle.main)
        collectionView?.register(nib, forCellWithReuseIdentifier: reuseIdentifier)

        collectionView?.backgroundColor = UIColor.clear
        
        collectionView?.delegate = self
        collectionView?.dataSource = self
    }
    
    // 为单元格加载动画
    func startAnimation(cell: UICollectionViewCell,index: Int,isShow: Bool = true){
        // 实例化pop动画对象
        let springAnimation = POPSpringAnimation(propertyNamed: kPOPViewCenter)
        // 设置对象属性参数
        springAnimation?.springBounciness = 10
        springAnimation?.springSpeed = 10
        // 设置开始延迟时间
        springAnimation?.beginTime = CACurrentMediaTime() + Double(index) * 0.1
        // 设置终点坐标位置
        let targetY = cell.center.y + CGFloat(isShow ? -300 : 300)
        springAnimation?.toValue = CGPoint(x: cell.center.x, y: targetY)
        // 添加动画
        cell.pop_add(springAnimation, forKey: nil)
    }
}

// 分类中实现数据源和代理协议
extension SWSquareController {
    // 行数
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        
        return sections
    }
    // 列数
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return cellDTOArray.count / self.sections
    }
    // 返回单元格
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! SWSquareViewCell
        cell.frame.origin.y += 300
        cell.cellDTO = self.cellDTOArray[indexPath.item + indexPath.section * 3]
        startAnimation(cell: cell, index: indexPath.item + 3 * indexPath.section)
        cellArray?.append(cell)
        return cell
    }
    // 实现点击效果
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        // 选中的单元格放大,其余的单元格缩小
        UIView.animate(withDuration: 1, delay: 0, options: [], animations: {
            var index = 0
            for cell in self.cellArray! {
                if index == (indexPath.row + indexPath.section * 3) {
                    // 选中的单元格放大
                    cell.transform = CGAffineTransform.init(scaleX: 5, y: 5)
                }else{
                    // 非选中单元格缩小
                    cell.transform = CGAffineTransform(scaleX: 0.2, y: 0.2)
                }
                index += 1
            }
            self.parent?.view.alpha = 0
        }, completion: {(_) -> () in
            // 淡化效果,完成后跳转页面
            if self.delegate != nil {
                self.delegate?.startComposeController()
            }
        })
    }
    
}











